package org.example.group5back.controller;

import org.example.group5back.entity.BaseResponse;
import org.example.group5back.entity.EquipmentEntity;
import org.example.group5back.entity.ResultBody;
import org.example.group5back.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class EquipmentController {

    @Autowired
    EquipmentService equipmentService;

    @RequestMapping("/saveEquipment")
    @ResponseBody
    @CrossOrigin(origins = "*",allowedHeaders = "*")
    public ResultBody savaEquipment(EquipmentEntity equip){
        System.out.println(equip.toString());
        if(equipmentService.saveEquipment(equip)){
            return new ResultBody(0,"success");
        }else{
            return new ResultBody(1,"fail");
        }
    }

    @RequestMapping("/deleteEquipment")
    @CrossOrigin(origins = "*",allowedHeaders = "*")
    public ResultBody deleteEquipment(int eq_id){
        if(equipmentService.deleteEquipment(eq_id)){
            return new ResultBody(0,"success");
        }else{
            return new ResultBody(1,"fail");
        }
    }

    @RequestMapping("/listEquipment")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public BaseResponse<EquipmentEntity> listEquipment(){
        List<EquipmentEntity> equipmentEntityList = equipmentService.finAllEquipment();
        return new BaseResponse<EquipmentEntity>(0,"ok",
                equipmentEntityList.size(),equipmentEntityList);
    }


    @RequestMapping("/selectEquipment")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public BaseResponse<EquipmentEntity>selectEquipment(@RequestParam(value="page",required=false) Integer page,
    @RequestParam(value = "limit",required = false) Integer limit){
        List<EquipmentEntity> eq_list = equipmentService.finAllEquipment();
        Integer pageNum = (page - 1) * limit;
        List<EquipmentEntity> show_eq_list = equipmentService.showPageLimit(pageNum,limit);
        return new BaseResponse<EquipmentEntity>(0,"ok",
                eq_list.size(),show_eq_list);
    }
    @RequestMapping("/selectByName")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public BaseResponse<EquipmentEntity>selectByName(@RequestParam(value="page",required=false) Integer page,
                                                     @RequestParam(value = "limit",required = false) Integer limit,
            @RequestParam(value = "eq_name",required = false) String eq_name
    ,@RequestParam(value = "eq_data",required = false) String eq_data
    ,@RequestParam(value = "eq_after_sale",required = false) String eq_after_sale
    ,@RequestParam(value = "eq_type",required = false) String eq_type){
        System.out.println(page);
        System.out.println(limit);
        if(eq_name.isEmpty()){
            System.out.println("eq_name is null");
            eq_name = null;
        }
        if(eq_data.isEmpty()){
            System.out.println("eq_data is null");
            eq_data = null;
        }
        if(eq_after_sale.isEmpty()){
            System.out.println("eq_people is null");
            eq_after_sale = null;
        }
        if(eq_type.isEmpty()){
            System.out.println("eq_type is null");
            eq_type = null;
        }

        List<EquipmentEntity> eq_list = equipmentService.selectByName(eq_name,eq_data,eq_after_sale,eq_type);

        List<EquipmentEntity> show_eq_list = selectByNameMySelf(eq_list,page,limit);

        return new BaseResponse<EquipmentEntity>(0,"ok",
                eq_list.size(),show_eq_list);
    }
    public static List<EquipmentEntity> selectByNameMySelf(List<EquipmentEntity> eq_list,Integer page,Integer limit){
        List<EquipmentEntity> show_eq_list = new ArrayList<EquipmentEntity>();
        if(page * limit > eq_list.size()){
            show_eq_list = eq_list.subList((page-1)*limit,eq_list.size());
        }else{
            show_eq_list = eq_list.subList((page-1)*limit,page*limit);
        }
        return show_eq_list;
    }
}
